<template>
	<!-- 充值 -->
	<view class="addCar">
		<u-navbar title="充值" title-color="#333" back-icon-color="#333"></u-navbar>
		<view class="addCar-bottom">
			<view class="addCar-content-box">
				<!--  -->
				<view class="addCar-carName-phone">
					<text class="addCar-phone">请选择客户：</text>
					<!-- <input class="addCar-input-one" type="text" v-model="carNumberIpt"/> -->
					<view class="addCar-input-one" @click="cusshow=true">
						{{infoCusText}}
						<text class="iconfont iconxiala1"></text>
					</view>
				</view>

				<view class="addCar-carName-phone">
					<text class="addCar-phone">余额：</text>
					<text class="addCar-input-one">{{balanceMoney}}</text>
					<!-- <input class="addCar-input-one" type="text" placeholder="余额" v-model="balanceMoney" maxlength="15" /> -->
				</view>

				<view class="addCar-carName-phone">
					<text class="addCar-phone">充值金额：</text>
					<input class="addCar-input-one" type="number" @input="rechargeMoneyUP" placeholder="请输入充值金额" v-model="rechargeMoney"
					 maxlength="11" />
				</view>
				<view class="addCar-carName-phone">
					<text class="addCar-phone">充值类型：</text>
					<!-- <input class="addCar-input-one" type="number" @ placeholder="请输入充值类型" v-model="rechargeType" /> -->
					<view class="addCar-input-one" @click="typeshow=true">
						{{rechargeType}}
						<text class="iconfont iconxiala1"></text>
					</view>
				</view>
				<view class="addCar-carName-phone">
					<text class="addCar-phone">备注：</text>
					<input class="addCar-input-one" type="text" placeholder="请输入备注" v-model="remarks" />
				</view>
			</view>
			<!--  -->
			<view class="addCar-reset-determine">
				<!-- <view class="addCar-reset" @click="reset">重置</view> -->
				<view class="addCar-determine" @click="determine">确定</view>
			</view>
		</view>
		<u-modal v-model="addCarLoginShow" content="请确认是否充值" :show-cancel-button="true" @confirm="addCarConfirm"></u-modal>
		<u-select v-model="cusshow" :default-value="cusIndex" :list="cusList" @confirm="cusOnConfirm">
		</u-select>
		<u-select v-model="typeshow" :default-value="typeIndex" :list="typeList" @confirm="typeOnConfirm">
		</u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: JSON.parse(uni.getStorageSync('userinfo')),
				componeryArr: [],
				addressNew: null,
				Index: 0,
				diasabledInput: true, //可读不可写
				rechargeMoney: '', //充值金额
				remarks: '', //备注
				addCarLoginShow: false,
				carshow: false,
				carType: '',
				infoCusText: '',
				cusshow: false,
				cusList: [],
				infoCus: '',
				cusIndex: [0],
				balanceMoney: '',
				rechargeType: '', //充值类型
				typeshow: false,
				typeList: [],
				infoType: '',
				typeIndex: [0],
			}
		},
		onShow() {
			this.queryCus()
			this.querytype()
		},
		methods: {


			rechargeMoneyUP() {
				var value = this.rechargeMoney;
				var num = this.clearNoNum(value);
				if(num<=0) return
				if(num>99999999.99){
					uni.showToast({
						title: '充值金额高于最大限制99999999.99',
						duration: 5000,
						icon: 'none',
					});
				}
				num=num>99999999.99?99999999.99:num
				this.rechargeMoney=num
			},
			// 确定
			determine() {
				if (this.infoCusText == '') return uni.showToast({
					title: '请选择客户',
					duration: 5000,
					icon: 'none',
				});
				if (this.rechargeMoney < 0) return uni.showToast({
					title: '请重新输入充值金额',
					duration: 5000,
					icon: 'none',
				});
				if (this.rechargeType == '') return uni.showToast({
					title: '请选择充值类型',
					duration: 5000,
					icon: 'none',
				});
				this.addCarLoginShow = true

			},
			addCarConfirm() {
				uni.request({
					url: this.baseUrl + '/orm/ormtb411/insertOrmtb411',
					method: 'POST',
					dataType: 'json',
					data: {
						czMoney: this.rechargeMoney, //充值金额
						Type: this.rechargeType, //充值类型
						ORMTB411CC01: this.userinfo.PERTB005CA02, //充值人
						ORMTB420CA08: this.remarks,
						ORMTB410ID: this.infoCus, //二级客户ID
						ORMTB400ID: this.userinfo.ORMTB400ID,
					},
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							uni.showToast({
								title: '充值成功',
								duration: 5000,
								icon: 'none',
							});
							uni.navigateBack({
								delta:1
							})
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 5000,
								icon: 'none',
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.data.message,
							duration: 5000,
							icon: 'none',
						});
					}
				});
			},
			cusOnConfirm(e) {
				this.cusIndex = [(this.cusList || []).findIndex((profile) => profile.value === e[0].value)]
				this.infoCus = e[0].value
				this.infoCusText = e[0].label
				this.balanceMoney = this.cusList[this.cusIndex].money
				this.cusshow = false;
			},
			queryCus() {
				this.cusList=[]
				uni.request({
					url: this.baseUrl + '/orm/ormtb411/selectOrmtb410ById',
					method: 'POST',
					dataType: 'json',
					data: {
						ORMTB400ID: this.userinfo.ORMTB400ID,
					},
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							var data = res.data.data || []
							data.forEach(item => {
								this.cusList.push({
									value: item.ORMTB410ID,
									money: item.ORMTB410CA03,
									label: item.ORMTB410CA00,
								})
							})
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 5000,
								icon: 'none',
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.data.message,
							duration: 5000,
							icon: 'none',
						});
					}
				});
			},
			typeOnConfirm(e) {
				this.typeIndex = [(this.typeList || []).findIndex((profile) => profile.value === e[0].value)]
				this.infoType = e[0].value
				this.rechargeType = e[0].label
				this.typehow = false;
			},
			querytype() {
				uni.request({
					url: this.baseUrl + '/orm/ormtb411/selectSet206ById',
					method: 'POST',
					dataType: 'json',

					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							var data = res.data.data || []
							data.forEach(item => {
								this.typeList.push({
									value: item.SETTB206CA00,
									label: item.SETTB206CA01,
								})
							})
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 5000,
								icon: 'none',
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.data.message,
							duration: 5000,
							icon: 'none',
						});
					}
				});
			},
			clearNoNum(value) {
				value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
				value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
				value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
				value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
				if (value.indexOf(".") < 0 && value != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
					value = parseFloat(value);
				}
				return value;
			},
			// 重置
			// reset() {
			// 	this.carNumberIpt = '' //车牌号
			// 	this.nameIpt = '' //车主姓名
			// 	this.phoneIpt = '' //联系方式
			// },
		}
	}
</script>

<style scoped>
	.addCar input {
		border: 0;
	}

	.addCar {
		width: 100%;
		min-height: 100vh;
		background-color: rgba(242, 242, 242, 1);
		overflow: hidden;
	}

	/* 列表 */

	/* 内容 */
	.addCar-content-box {
		width: 100%;
		background: rgba(255, 255, 255, 1);
		padding: 0 24rpx;
	}

	.addCar-carName-phone {
		border-bottom: 2rpx solid rgba(230, 230, 230, 1);
		padding: 18rpx 0;
	}

	.addCar-input-one {
		width: 310rpx !important;
		display: inline-block;
		margin-bottom: 0 !important;
		padding: 0 !important;
		height: 44rpx !important;
		/* margin-left: 0.1rem; */
		color: rgba(51, 51, 51, 1);
		font-size: 24rpx;
		text-align: center;
	}

	.addCar-phone {
		width: 238rpx;
		display: inline-block;
		font-size: 26rpx;
		color: rgba(102, 102, 102, 1);
		/* background: #000000; */
	}

	.addCar-phone-placeholder {
		font-size: 26rpx;
		color: rgba(102, 102, 102, .5);
	}

	.addCar-dun {
		color: rgba(51, 51, 51, 1);
		font-size: 26rpx;
	}

	/* 按钮 */
	.addCar-reset-determine {
		margin-top: 167rpx;
		display: flex;
	}

	.addCar-reset-determine>view {
		width: 300rpx;
		margin: 0 auto;
		text-align: center;
		line-height: 65rpx;
		color: rgba(255, 255, 255, 1);
		font-size: 28rpx;
		border-radius: 28rpx 28rpx 28rpx 28rpx;
	}

	.addCar-reset {
		width: 159rpx;
		background: rgba(234, 197, 15, 1);
		text-align: center;
		margin-left: 170rpx;
	}

	.addCar-determine {
		background: rgba(0, 161, 244, 1);
		margin-left: 60rpx;
	}

	.addCar-phone-car {
		display: inline-block;
		width: 310rpx;
		height: 44rpx;
		line-height: 44rpx;
		/* border: 1px solid rgba(0, 0, 0, .2); */
		/* border-radius: 3px; */
		text-align: center;
		vertical-align: middle;
		font-size: 26rpx;
	}

	input::placeholder {
		color: rgba(102, 102, 102, .5);
	}
</style>
